//
//  WBHomeCell.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/17.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBHomeCell.h"
#import "WBStatuseInfoViewModel.h"
#import "WBStatusesModel.h"
#import "WBPicCollectionView.h"
#import <YYText.h>

#import "WBTestWebViewController.h"


@interface WBHomeCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImage;

@property (weak, nonatomic) IBOutlet UILabel *name;

@property (weak, nonatomic) IBOutlet UILabel *create_time;

@property (weak, nonatomic) IBOutlet UILabel *sourse;

@property (weak, nonatomic) IBOutlet YYLabel *lbl_text;

@property (weak, nonatomic) IBOutlet UIImageView *verified_type;

@property (weak, nonatomic) IBOutlet UIImageView *mbrank;

@property (weak, nonatomic) IBOutlet WBPicCollectionView *pictureCollectionV;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *picFlowLayout;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *picCollectionH;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *picCollectionW;



@property (weak, nonatomic) IBOutlet NSLayoutConstraint *text_retweet_distance;

@property (weak, nonatomic) IBOutlet YYLabel *tweetedText;





@end

///屏幕与微博间距
static CGFloat margin = 8;
///图片之间的间距
static CGFloat imgMargin = 2;
static CGFloat maxWideth;
static CGFloat itemWidth;


@implementation WBHomeCell



- (void)awakeFromNib {
    [super awakeFromNib];
    
    maxWideth = kUISCRRENW - 2 * margin;
    itemWidth = (maxWideth - 2 * imgMargin) / 3 ;
    
//    self.backgroundColor = [UIColor cz_randomColor];
    
    //设置内容
    self.lbl_text.numberOfLines = 0;
    self.lbl_text.preferredMaxLayoutWidth = maxWideth;

    self.tweetedText.numberOfLines = 0;
    self.tweetedText.preferredMaxLayoutWidth = maxWideth;
    
    //layout
//    self.picFlowLayout.itemSize = CGSizeMake(itemWidth, itemWidth);
    self.picFlowLayout.minimumLineSpacing = imgMargin;
    self.picFlowLayout.minimumInteritemSpacing = imgMargin;
    
    /*
     接受事件回调
     label.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
     NSLog(@"tap text range:...");
     };
     */
    //响应高亮文本事件
    self.lbl_text.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        
        NSString *urlStr = [text.string substringWithRange:range];
        if ([urlStr hasPrefix:@"http"]) {
            
            WBTestWebViewController *webVc = [[WBTestWebViewController alloc]init];
            
            webVc.urlStr = urlStr;
            
            [[self findNavController] pushViewController:webVc animated:YES];
        }
        
    };
    self.tweetedText.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        
        NSString *urlStr = [text.string substringWithRange:range];
        if ([urlStr hasPrefix:@"http"]) {
            
            WBTestWebViewController *webVc = [[WBTestWebViewController alloc]init];
            
            webVc.urlStr = urlStr;
            
            [[self findNavController] pushViewController:webVc animated:YES];
        }
        
    };
}

-(void)setModel:(WBStatuseInfoViewModel *)model{

    _model = model;
    
    [self.iconImage sd_setImageWithURL:model.url];
    self.name.text = model.statuse.user.name;
    self.create_time.text = model.sendTime;
    self.sourse.text = model.weiboSource;
//    self.lbl_text.text.at = model.originalText;
    self.lbl_text.attributedText = model.originalText;
    
    self.verified_type.image = model.verified_typeImg;
    self.mbrank.image = model.mbrankImg;
    
    NSInteger count = model.imgURLs.count;
    
    self.text_retweet_distance.constant = (count == 0) ? 0 : 8;
    
    self.tweetedText.attributedText = model.retweetedText;
    
    self.pictureCollectionV.picUrls = model.imgURLs;
    NSArray *newSize = [self picSize:count];
    
    self.picCollectionW.constant = [newSize[0] CGSizeValue].width;
    self.picCollectionH.constant = [newSize[0] CGSizeValue].height;
    
    self.picFlowLayout.itemSize = [newSize[1] CGSizeValue];
    
    [self.pictureCollectionV layoutIfNeeded];
    

    
}
///重新约束尺寸
/**
 存放图片尺寸
 */
-(NSArray *)picSize:(NSInteger)count {

    if (count == 0) {
        NSValue *size = [NSValue valueWithCGSize:CGSizeMake(1, 1)];
        return @[size ,size];
    }
    
    if (count == 1) {
        
        UIImage *singleImg = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:self.model.statuse.pic_urls.lastObject.wap360];
        
        NSValue *itemSize = [NSValue valueWithCGSize:singleImg.size];
        
        return @[itemSize ,itemSize];
    }
    
    if (count == 4) {
        
        NSValue *itemSize = [NSValue valueWithCGSize:CGSizeMake(itemWidth, itemWidth)];
        NSValue *picSize = [NSValue valueWithCGSize:CGSizeMake(itemWidth * 2 + imgMargin + 1, itemWidth * 2 + imgMargin + 1)];
        return @[picSize ,itemSize];
    }
    
    NSInteger row = (count - 1) / 3 + 1;
    CGFloat H = row * itemWidth + (row - 1) * imgMargin;
    NSValue *itemSize = [NSValue valueWithCGSize:CGSizeMake(itemWidth, itemWidth)];
    NSValue *picSize = [NSValue valueWithCGSize:CGSizeMake(maxWideth, H)];
    return @[picSize ,itemSize];
}


@end







